#!/usr/bin/env bash

# mysql配置文件
function mysql_config() {
  # 将配置写进配置文件
  echo_sleep "开始写入配置文件……"

  cat >${applicationPath}/conf/my.cnf <<-EOF
[client]
default-character-set = utf8mb4

[mysqld]
pid-file= /var/run/mysqld/mysqld.pid
socket= /var/run/mysqld/mysqld.sock
datadir= /var/lib/mysql
secure-file-priv= NULL

# Custom config should go here
# 字符集
character_set_server=utf8mb4
collation-server=utf8mb4_general_ci
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

# 是否对sql语句大小写敏感，0:大小写敏感,1:忽略大小写区别。，只能在初始化服务器时配置。禁止在服务器初始化后更改
# 设置为2时，表名和数据库名按声明存储，但以小写形式进行比较
lower_case_table_names = 1

# 最大连接数
max_connections = 1000

# Innodb缓存池大小
innodb_buffer_pool_size = 4G

# 表文件描述符的缓存大小
table_open_cache_instances=1
table_open_cache=2000
table_definition_cache=2000
#max_allowed_packet=600M
#wait_timeout=2880000
#interactive_timeout = 2880000
#!includedir /etc/mysql/conf.d/
EOF

  echo_sleep "配置文件写入完成！\n"
}

# 判断容器是否启动成功
function mysql_successOrNot() {
  echo_sleep "docker run命令:\n $dockerRunCmd \n" 2
  if [[ -n $(docker ps -q -f "name=^$application$") ]]; then
    echo_red "$application 容器启动成功"
    echo -e "\t端口号：$port"
    echo -e "\t版本号：$version"
    echo -e "\t容器名称：$application"
    echo -e "\t密码：$passwd"
    echo -e "\t本地映射路径：$applicationPath"
    echo -e "进入容器使用命令：docker exec -it $application /bin/bash"
  else
    log_error "$application 容器启动失败,请检查docker run命令是否有误,端口是否被占用"
  fi
}

# 安装Mysql
function mysql_install() {
  application="mysql"                            # 应用名称,docker ps中显示的名称
  image="mysql"                                  # docker仓库中镜像名称
  applicationPath="/opt/${image}/${application}" # 本地映射文件路径
  port="3306"                                    # 对外映射的端口号
  passwd="1234"                                  # 密码
  version="latest"                               # 镜像版本号,默认为最新版本
  dockerRunCmd=""                                # 最后执行的docker run命令

  echo -e "\n=========================开始安装MySql========================="

  echo_yellow "1.检测是否存在 ${image} 容器"
  docker_delExisting ${image}

  echo_yellow "2.开始拉取镜像文件"
  docker_pull

  echo_yellow "3.开始创建本地映射目录"
  create_file_path conf logs data mysql-files

  echo_yellow "4.开始创建配置文件"
  mysql_config

  echo_yellow "5.创建并启动docker容器"
  port=$(read_input "请输入需要映射的端口(默认为 ${port})：" ${port})
  application=$(read_input "请输入容器的名称(默认为 ${application})：" ${application})
  passwd=$(read_input "请输入 ${application} 数据库密码(默认为 ${passwd})：" ${passwd})

  echo -e "${application}启动中,请等待......"

  dockerRunCmd="docker run --restart=always --privileged=true \
	-v ${applicationPath}/data/:/var/lib/mysql \
	-v ${applicationPath}/logs/:/var/log/mysql \
	-v ${applicationPath}/conf/:/etc/mysql/conf.d \
	-v ${applicationPath}/mysql-files:/var/lib/mysql-files \
	-v /etc/localtime:/etc/localtime:ro \
	-e MYSQL_ROOT_PASSWORD=$passwd \
	-p $port:3306 --name $application -d $image:$version &> /dev/null"

  eval $dockerRunCmd

  echo_yellow "6.检测容器是否启动成功"
  mysql_successOrNot

  echo -e "=========================安装MySql结束========================="
}
